*** Copy thy neighbour: Spatial interdependences in the democracy-repression nexus ***
*** Journal of Human Rights ***
*** Created by Roman-Gabriel Olar on 01/05/2019 ***
*** Last modified on 25/08/2023 ***

*** Set working directory

cd "set your own working directory"

*** TABLE A1 ***

*** Simple S-OLS (MODEL A1) ***
use "MainData.dta", clear
reg theta_mean l_polyarchy SLreg_fariss_t1 SLreg_polyarchy_t1 l_gdp_gro l_lngdppc l_lnpop civil_war international_war l_resdep l_fariss ccode_fe* y_fe*
outreg2 using sols, replace label word


*** S-ML (MODEL A2) ***

clear
pr drop _all
set matsize 8000
set more off
version 9                                                         

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 5870

clear
spatwmat using "W_region.dta", name(W)standardize
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)

drop _all

use "MainData.dta", clear

global Y theta_mean
global X l_polyarchy l_fariss SLreg_polyarchy_t1 l_gdp_gro l_lngdppc l_lnpop civil_war international_war l_resdep ccode_fe* y_fe*
global Z l_fariss
mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)
matrix SL = W*Z
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) vce(oim)
ml init OLSb, skip
ml init rho:_cons=0
ml init sigma:_cons=`OLSsigma'
ml max, difficult ltolerance(1e-8) tolerance(1e-8)
outreg2 using sml2, replace label word

*** S-ML (MODEL A3) ***
clear
pr drop _all
set matsize 8000
set more off
version 9                                                         

program define splag_ll

args lnf mu rho sigma
tempvar A rSL
gen `A'= ones - `rho'*EIGS1
gen `rSL'=`rho'*SL1
qui replace `lnf'= ln(`A') + ln(normden($ML_y1-`rSL'-`mu', 0, `sigma'))
end

global nobs = 5870

clear
spatwmat using "W_region.dta", name(W)standardize
matrix eigenvalues eig1 imaginaryv = W
matrix eig2 = eig1'
matrix ones=J($nobs,1,1)

drop _all

use "MainData.dta", clear

global Y theta_mean
global X l_polyarchy l_fariss SLreg_polyarchy_t1 l_gdp_gro l_lngdppc l_lnpop civil_war international_war l_resdep ccode_fe* y_fe*
global Z theta_mean
mkmat $Y, matrix(Y)
mkmat $Z, matrix(Z)
matrix SL = W*Z
svmat SL, n(SL)
svmat eig2, n(EIGS)
svmat ones, n(ones)

qui regress $Y $X
matrix OLSb=e(b)
local OLSsigma=e(rmse)

ml model lf splag_ll (mu: $Y=$X) (rho:) (sigma:), technique(dfp) vce(oim)
ml init OLSb, skip
ml init rho:_cons=0
ml init sigma:_cons=`OLSsigma'
ml max, difficult ltolerance(1e-8) tolerance(1e-8)
outreg2 using sml, append label word

